/**
 * 
 */
package com.wistech.search.solr;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.util.CollectionUtils;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.dic.Dictionary;

import com.google.common.collect.Sets;

/**
 * @author Liuye
 * 
 */
@SuppressWarnings("serial")
public class AddWordServlet extends GenericServlet {

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.GenericServlet#service(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse)
	 */
	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		String wordStr = req.getParameter("word");
		if (wordStr != null && !wordStr.trim().isEmpty()) {
			wordStr = wordStr.replaceAll(" ", "");
			String[] words = wordStr.split(",");
			Collection<String> wordList = Sets.newHashSet();
			CollectionUtils.mergeArrayIntoCollection(words, wordList);
			Dictionary.initial(Configuration.getInstance());
			Dictionary.getSingleton().addWords(wordList);
		}
		res.getOutputStream().print("OK");
	}
}
